package com.ithailong.reconsitutionmassion.dao.Impl;

import com.ithailong.reconsitutionmassion.dao.FileDao;
import com.ithailong.reconsitutionmassion.entity.FileModel;
import com.ithailong.reconsitutionmassion.util.Dbutils;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * author:hlc
 * date:2023/10/30
 *
 * @author 17930
 */
@Repository
public class FileDaoImpl implements FileDao {
   private Dbutils dbutils = new Dbutils();

   @Override
   public List<FileModel> selectAllFile(int pageNum, int pageSize, FileModel fileModel) {
      StringBuilder sql = new StringBuilder("select * from kfm_file ");
      StringBuilder where = new StringBuilder();
      List params = new ArrayList();
      if (StringUtils.hasText(fileModel.getName())) {//按照文件名称查询
         where.append(" where name = ? ");
         params.add(fileModel.getName());
      }
      if (!ObjectUtils.isEmpty(fileModel.getUploadIp())) {
         if (where.length() == 0) {
            where.append(" where upload_ip = ?");
         } else {
            where.append(" and upload_ip = ?");
         }
         params.add(fileModel.getUploadIp());
      }
      sql.append(where);
      sql.append(" limit ?,? ");
      params.add((pageNum - 1) * pageSize);
      params.add(pageSize);
      try {
         return dbutils.selectList(sql.toString(), FileModel.class, params.toArray());
      } catch (SQLException e) {
         e.printStackTrace();
         return null;
      }
   }

   @Override
   public int selectTotal(FileModel fileModel) {
      StringBuilder sql = new StringBuilder("select count(*) from kfm_file ");
      StringBuilder where = new StringBuilder();
      List params = new ArrayList();
      if (StringUtils.hasText(fileModel.getName())) {
         where.append(" where name = ?");
         params.add(fileModel.getName());
      }
      if (!ObjectUtils.isEmpty(fileModel.getUploadIp())) {
         if (where.length() == 0) {
            where.append(" where upload_ip = ?");
         } else {
            where.append(" and upload_ip = ?");
         }
         params.add(fileModel.getUploadIp());
      }
      sql.append(where);
      try {
         return dbutils.selectTotal(sql.toString(), params.toArray());
      } catch (SQLException e) {
         e.printStackTrace();
         return 0;
      }
   }

   @Override
   public void saveFile(FileModel fileModel) {
      String sql = "insert into kfm_file(name,size,create_time,upload_ip,download_link,enable)" +
              "values(?,?,?,?,?,?)";
      try {
         dbutils.update(sql, fileModel.getName(), fileModel.getSize(),
                 fileModel.getCreateTime(), fileModel.getUploadIp(),
                 fileModel.getDownloadLink(),fileModel.getEnable());
      } catch (SQLException e) {
         e.printStackTrace();
      }
   }

   @Override
   public void deleteFile(Integer id) {
      String sql = "delete from kfm_file where id = ?";
      try {
         dbutils.update(sql, id);
      } catch (SQLException e) {
         e.printStackTrace();
      }
   }
}
